shanbay.mail = (function() {

  //发送短信
  this.mail_to = function(users, title, content, callback) {
    console.log("SE: mail to: " + JSON.stringify(users));
    
    if (typeof users == "string") {
      users = [ users ];
    }

    var total = users.length, current = 0;

    for ( var i in users) {
      $.ajax({
        type : "POST",
        url : "http://www.shanbay.com/17mail/compose/",
        data : {
          recipient : users[i],
          subject : title,
          body : content,
          csrfmiddlewaretoken : $.cookie("csrftoken"),
        },
        success : function() {
          if (++current == total && callback) {
            callback();
          }
        }
      });
    }
  };

  this.enhance_mail = function() {
    //短信增强
    //回复提醒
    if ($("div#content-title h2").text() == "浏览短信") {
      if ($("div.message-headers strong").text() == "[扇贝助手]新回复提醒") {
        var $msg = $("div.message");
        var url = $msg.text().match(/链接：\[ ([^\]]+) \]/)[1];
        $msg.find("p a.button").prop({
          href : url,
          target : "_blank",
        }).text("打开链接");
      }
    }
  };

  return this;
})();

$(function() {
  shanbay.mail.enhance_mail();
});
